var storage = {
	setItem : function(name, value) {
		var Days = 30;
		var exp = new Date();
		exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
		document.cookie = name + "=" + escape(value) + ";expires="
				+ exp.toGMTString();
	},
	getItem : function(name) {
		var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (arr = document.cookie.match(reg))
			return unescape(arr[2]);
		else
			return null;
	}
}
var help = {
	showTip : function(msg, second) {
		if (second == undefined || second == null) {
			second = 1;
		}
		if (msg.indexOf("-") > -1) {
			msg = msg.split("-")[1];
		}
		$(".dialog").html(msg);
		$("#dialog").show();
		setTimeout(function() {
			$("#dialog").hide();
		}, second * 1000);
	}
}

var wapAjax = {
	type : "POST",
	url : "",// 必须要初始化
	async : false,
	beforeSend : function() {
	},
	error : function(xhr, ajaxOptions, thrownError) {
		return false;
	},
	complete : function() {
	},

	baseRequest : function(ajaxType, ajaxUrl, ajaxData, ajaxAsync, bsCallBack,
			sucCallBack, errCallBack, cmpCallBack) {
		$.ajax({
			type : ajaxType,
			url : ajaxUrl,
			data : ajaxData,
			async : ajaxAsync,
			dataType : "text",
			cache : false,
			crossDomain : true,
			beforeSend : bsCallBack,
			success : sucCallBack,
			error : errCallBack,
			complete : cmpCallBack
		});
	},

	commonRequest : function(ajaxData, sucCallBack) {
		this.baseRequest(this.type, this.url, ajaxData, this.async,
				this.beforeSend, sucCallBack, this.error, this.complete);
	},

	commonPostRequest : function(ajaxData, sucCallBack) {
		this.baseRequest("POST", this.url, ajaxData, this.async,
				this.beforeSend, sucCallBack, this.error, this.complete);
	},
	userCommonRequest : function(ajaxData, loadingText, sucCallBack) {
		this.error = function(xhr, ajaxOptions, thrownError) {
			// Tools.showMyDialog("网络不给力，请稍后再试!", 3000);
			return false;
		};
		this.beforeSend = function() {
			$.mobile.loading('show', {
				text : loadingText,
				textVisible : "true",
				theme : "a",
				textonly : "true",
				html : ""
			});
		};
		this.baseRequest(this.type, this.url, ajaxData, this.async,
				this.beforeSend, sucCallBack, this.error, this.complete);
	},

	userRequestAddCmp : function(ajaxData, loadingText, sucCallBack) {
		this.error = function(xhr, ajaxOptions, thrownError) {
			// Tools.showMyDialog("网络不给力，请稍后再试!", 3000);
			return false;
		};
		this.beforeSend = function() {
			$.mobile.loading('show', {
				text : loadingText,
				textVisible : "true",
				theme : "a",
				textonly : "true",
				html : ""
			})
		};
		this.complete = function() {
			$.mobile.loading('hide');
		};
		this.baseRequest(this.type, this.url, ajaxData, this.async,
				this.beforeSend, sucCallBack, this.error, this.complete);
	}
}

var common = {
	location:function(){
		return  document.location.protocol+"//"+location.host; 
	},
	// 字符串转成Date类型，字符串格式为"2005-08-12 12:23:44"
	ParseDateTime : function(dateTime) {
		var mr = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/
				.exec(dateTime);
		if (mr) {
			var d = new Date(parseInt(mr[1], 10), parseInt(mr[2], 10) - 1,
					parseInt(mr[3], 10), parseInt(mr[4], 10), parseInt(mr[5],
							10), parseInt(mr[6], 10));
			var opentime = d.getMonth() + 1 + "月" + d.getDate() + "日"
					+ d.getHours() + ":" + d.getMinutes();
			return opentime;
		} else {
			alert("格式不正确!");
		}
	},
	checkNull:function (str){
		
		if(str==null){	
			return "";
		}
		return str;	
	},
	// 四舍六入 五留双
	prizeRound : function(a) {
		var r = /^(\d+\.\d[02468])50*$/g;
		if (typeof (a) == 'number') {
			a = a.toString();
		}
		if (r.test(a)) {
			a = a.replace(r, "$1");
		}
		return parseFloat(a).toFixed(2);
	},
	getDateStr : function(n) {
		return n.substr(0, 4) + '-' + n.substr(4, 2) + '-' + n.substr(6, 2)
	},
	getWkByIssue : function(n) {
		var dt = this.str2date(n);
		var wk = [ '周日', '周一', '周二', '周三', '周四', '周五', '周六' ];
		return wk[dt.getDay()];
	},
	getTime : function(n) {
		var dt = this.str2date(n);
		var hh = dt.getHours();
		if (hh * 1 < 10) {
			hh = "0" + hh
		}
		var mm = dt.getMinutes();
		if (mm * 1 < 10) {
			mm = "0" + mm
		}
		return hh + ":" + mm;
	},
	toDecimal2 : function(x) {
		var f = parseFloat(x);
		if (isNaN(f)) {
			return false;
		}
		var f = Math.round(x * 100) / 100;
		var s = f.toString();
		var rs = s.indexOf('.');
		if (rs < 0) {
			rs = s.length;
			s += '.';
		}
		while (s.length <= rs + 2) {
			s += '0';
		}
		return s;
	},
	str2date : function(s) {
		// / <summary>字符转date</summary>
		// / <param name="s" type="String">2012-01-01
		// 12:00:00或者20120101120000</param>
		s = typeof s == "number" ? s + "" : s;
		if (typeof (s) == "string") {
			$.trim(s);
			s = s.replace(/[\/\s\:]/gi, "-");
			if (s.match(/\-/) != null) {
				md = s.split("-");
				md[1] = md[1] * 1 - 1;
				if (md.length > 3) {
					s = new Date(md[0], md[1], md[2], md[3], md[4], md[5])
				} else {
					s = new Date(md[0], md[1], md[2]);
				}
			} else {
				md = s.split("");
				var mdy = ("" + md[0] + md[1] + md[2] + md[3]) * 1;
				var mdm = ("" + md[4] + md[5]) * 1 - 1;
				var mdd = ("" + md[6] + md[7]) * 1;
				if (md.length > 10) {
					var mdhh = ("" + md[8] + md[9]) * 1;
					var mdmm = ("" + md[10] + md[11]) * 1;
					var mdss = ("" + md[12] + md[13]) * 1;
					s = new Date(mdy, mdm, mdd, mdhh, mdmm, mdss);
				} else {
					s = new Date(mdy, mdm, mdd);
				}
			}
		}
		return s;
	},
	/** 获得路径参数 * */
	getQueryStringRegExp : function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null)
			return decodeURI(r[2]);
		return null;
	},
	setCookie : function setCookie(name, value, expires) {
		if (expires == undefined) {
			expires = 10 * 60 * 1000;
		}
		var host = window.location.host;
		var domain = host;
		if (host.split('.').length > 2) {
			var urls = host.split('.');
			domain = urls[urls.length - 2] + "." + urls[urls.length - 1];
		}
		var exp = new Date();
		exp.setTime(exp.getTime() + expires);
		document.cookie = name + "=" + escape(value) + ";expires="
				+ exp.toGMTString() + ";domain=" + domain + ";path=/;";
	},
	getCookie : function getCookie(name) {
		var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (arr = document.cookie.match(reg))
			try {
				return decodeURIComponent(arr[2]);
			} catch (e) {
				return getCookieNew(name);
			}

		else
			return "";
	},
	PraseTime : function(datetime) {
		var today = new Date();
		var year =  parseInt(datetime.year)+ 1900;  
		var s = today.getTime() - datetime.time;// 毫秒
		/*if (s < 1000 * 60 * 60) {
			return parseInt(s / 1000 / 60) + "分钟前";
		} else if (s < 1000 * 60 * 60 * 24) {
			return parseInt(s / 1000 / 60 / 60) + "小时前";
		} else {*/
			var month = (parseInt(datetime.month) + 1);
			var day = (datetime.date);
			if (month < 10) {
				month = "0" + month;
			}
			if (day < 10) {
				day = "0" + day;
			}
			return year+"."+month + "." + day;
	},
	PraseTimeall : function(datetime) {
	    var year =  parseInt(datetime.year)+ 1900;  
	    var month = (parseInt(datetime.month) + 1);
		var day = datetime.date;
		if (month < 10) {
			month = "0" + month;
		}
		if (day < 10) {
			day = "0" + day;
		}
		var hour = datetime.hours;
		var minute=datetime.minutes;
		var second=datetime.seconds;
	    minute = minute < 10 ? ('0' + minute) : minute;    
	    second = second < 10 ? ('0' + second) : second;   
	    return year + '-' + month + '-' + day+' '+hour+':'+minute+':'+second;  
	},
	getCurrentDay : function() {
		var today = new Date();
		var year = today.getFullYear();
		var month = (today.getMonth() + 1) > 9 ? (today.getMonth() + 1) : "0"
				+ (today.getMonth() + 1);
		var day = today.getDate() > 9 ? today.getDate() : "0" + today.getDate();
		var todayStr = year + "" + month + "" + day;
		return todayStr;
	}
}

Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1,
		// month
		"d+" : this.getDate(),
		// day
		"h+" : this.getHours(),
		// hour
		"m+" : this.getMinutes(),
		// minute
		"s+" : this.getSeconds(),
		// second
		"q+" : Math.floor((this.getMonth() + 3) / 3),
		// quarter
		"S" : this.getMilliseconds()
	// millisecond
	};
	if (/(y+)/.test(format) || /(Y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	}
	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}
/* 扩展删除数组 */
Array.prototype.removebyValue = function(val) {
	var dx;
	for (var i = 0, n = 0; i < this.length; i++) {
		if (this[i] == val) {
			dx = i;
			break;
		}
	}
	this.removebyIndex(dx);
}
Array.prototype.removebyIndex = function(dx) {
	if (isNaN(dx) || dx > this.length) {
		return false;
	}
	for (var i = 0, n = 0; i < this.length; i++) {
		if (this[i] != this[dx]) {
			this[n++] = this[i]
		}
	}
	this.length -= 1
}, Array.prototype.getindex = function(el) {
	for (var i = 0, n = this.length; i < n; i++) {
		if (this[i] === el) {
			return i;
		}
	}
	return -1;
};